package org.example.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.system.common.result.Result;
import org.example.system.model.entity.BizTenant;
import org.example.system.model.form.TenantForm;
import org.example.system.model.query.TenantPageQuery;
import org.example.system.model.vo.TenantPageVo;
import org.example.system.service.BizTenantService;
import org.modelmapper.ModelMapper;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@Tag(name = "09.租户管理")
@RestController
@RequestMapping("/api/v1/tenant")
@Slf4j
public class BizTenantController {

    @Resource
    private BizTenantService tenantService;

    private ModelMapper modelMapper = new ModelMapper();

    /**
     * 多条件分页查询租客信息
     * @param tenantPageQuery 查询条件对象
     * @return 分页查询结果
     */
    @Operation(summary = "多条件分页查询租客信息")
    @PostMapping("/search")
    public Result<Object> queryTenantByPage(@RequestBody TenantPageQuery tenantPageQuery) {
        LambdaQueryWrapper<BizTenant> criteria = new LambdaQueryWrapper<>();
        if(StringUtils.hasLength(tenantPageQuery.getKeywords())) {
            /*criteria.like(Tenant::getName, tenantPageQuery.getKeyword())
                    .or()
                    .like(Tenant::getPhone, tenantPageQuery.getKeyword())
                    .or()
                    .like(Tenant::getNickName, tenantPageQuery.getKeyword());*/

            criteria.like(BizTenant::getName, tenantPageQuery.getKeywords())
                    .or(k-> k.like(BizTenant::getPhone, tenantPageQuery.getKeywords()))
                    .or(f -> f.like(BizTenant::getNickName, tenantPageQuery.getKeywords()));
        }
        if(tenantPageQuery.getStatus() != null && tenantPageQuery.getStatus() != -1) {
            criteria.eq(BizTenant::getStatus, tenantPageQuery.getStatus());
        }
        int pageNum = tenantPageQuery.getPageNum();
        int pageSize = tenantPageQuery.getPageSize();
        Page<BizTenant> page = new Page<>(pageNum, pageSize);
        Page<BizTenant> tenantPage = tenantService.page(page, criteria);
        TenantPageVo tenantPageVo = new TenantPageVo();
        tenantPageVo.setTotal(tenantPage.getTotal());
        tenantPageVo.setList(tenantPage.getRecords());
        return Result.success(tenantPageVo);
    }

    @Operation(summary = "删除租客信息")
    @DeleteMapping("/remove/{id}")
    public Result<Object> removeTenant(@PathVariable(value = "id") Integer id) {
        tenantService.removeById(id);
        return Result.success();
    }

    @Operation(summary = "获取租户详细")
    @PostMapping("/detail/{id}")
    public Result addTenant(@PathVariable(name = "id") Integer id) {
        LambdaQueryWrapper<BizTenant> criteria = new LambdaQueryWrapper<>();
        criteria.eq(BizTenant::getId, id);
        Optional<BizTenant> tenantOpt = tenantService.getOneOpt(criteria);
        if(tenantOpt.isPresent()){
            BizTenant tenant = tenantOpt.get();
            return Result.success(tenant);
        }
        return Result.failed("租户ID不存在");
    }

    @Operation(summary = "修改租户信息")
    @GetMapping("/update")
    public Result updateTenant(@RequestBody TenantForm tenantForm) {
        if(tenantForm.getId() == null) {
            return Result.failed("租户ID不能为空");
        }
        BizTenant tenantPo = modelMapper.map(tenantForm, BizTenant.class);
        tenantService.updateById(tenantPo);
        return Result.success("修改租户成功");
    }


    @Operation(summary = "添加租户信息")
    @GetMapping("/add")
    public Result addTenant(@RequestBody TenantForm tenantForm) {
        BizTenant tenantPo = modelMapper.map(tenantForm, BizTenant.class);
        tenantService.updateById(tenantPo);
        return Result.success("完善租户信息成功");
    }
}
